import Vue from 'vue'
import Router from 'vue-router'
// import order from './views/order/order.vue'
// import report from './views/report/report.vue'
const login = () => import(/* webpackChunkName：'login_home-welcome' */ './views/login/login.vue')
const home = () => import(/* webpackChunkName：'login_home-welcome' */ './views/home/home.vue')
const welcome = () => import(/* webpackChunkName：'login_home-welcome' */ './views/home/components/welcome.vue')
const users = () => import(/* webpackChunkName：'users_rights_roles' */ './views/users/users.vue')
const rights = () => import(/* webpackChunkName：'users_rights_roles' */ './views/rights/rights.vue')
const roles = () => import(/* webpackChunkName：'users_rights_roles' */ './views/rights/roles.vue')
const cate = () => import(/* webpackChunkName：'cate_params' */ './views/goods/cate.vue')
const params = () => import(/* webpackChunkName：'cate_params' */ './views/goods/params.vue')
const goodslist = () => import(/* webpackChunkName：'goodslist_addgoods' */ './views/goods/list.vue')
const addgoods = () => import(/* webpackChunkName：'goodslist_addgoods' */ './views/goods/add.vue')
const order = () => import(/* webpackChunkName：'order_report' */ './views/order/order.vue')
const report = () => import(/* webpackChunkName：'order_report' */ './views/report/report.vue')

Vue.use(Router)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/home',
    component: home,
    redirect: '/welcome',
    children: [
      {
        // 欢迎页面
        path: '/welcome', component: welcome
      },
      {
        // 用户列表组件
        path: '/users', component: users
      },
      {
        // 权限管理组件
        path: '/rights', component: rights
      },
      {
        // 角色管理组件
        path: '/roles', component: roles
      },
      {
        // 商品分类组件
        path: '/categories', component: cate
      },
      {
        // 分类参数组件
        path: '/params', component: params
      },
      {
        // 商品列表组件
        path: '/goods', component: goodslist,
      },
      {
        // 商品添加页面
        path: '/goods/add', component: addgoods,
      },
      {
        // 订单列表页面
        path: '/orders', component: order,
      },
      {
        // 报表页面
        path: '/reports', component: report,
      }
    ]
  }
]

const router = new Router({
  routes
})

// 路由导航守卫
router.beforeEach((to, from, next) => {
// to是将要访问的路径
// from是哪个路径跳转过来
// next是放行函数 2种使用 next() next('/login')
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
